<?php
class PersonalAction extends AppAction
{
    function __construct ()
    {
        parent::__construct();
        require_once APP_LIB_PATH . 'adminLib.php';
        $this->lib = new AdminLib();
        AdminVisitor::checkLogin();
    }
    //http://apple.zhaosheng.com/admin.php/Personal/editpassword
    public function editpassword ()
    {
        ViewData::$form_hint = '';
        if ($_POST) {
            try {
                $this->editPasswordForm();
                $this->editPasswordLogic();
                ViewData::$form_hint = "修改成功";
            } catch (Exception $e) {
                ViewData::$form_hint = $e->getMessage();
            }
        }
        $info = AdminSession::getAdminSessionInfo();
        ViewData::$form_data = $this->lib->getByAdminName($info['AdminName']);
        BaseAction::loadView("admin/adminEditPassword");
    }
    private function editPasswordForm ()
    {
        ViewData::$form_data = BaseAction::parseForm("POST", 
        ViewData::$form_data);
        BaseAction::checkForm('OldPassword', 'POST', '旧密码不能为空', 
        array(2, 6, 16, "旧密码长度错误"), FALSE, FALSE);
        BaseAction::checkForm('NewPassword', 'POST', '新密码不能为空', 
        array(2, 6, 16, "新密码长度错误"), FALSE, FALSE);
        BaseAction::checkForm('ReNewPassword', 'POST', '确认新密码不能为空', 
        array(2, 6, 16, "确认新密码长度错误"), FALSE, FALSE);
        return;
    }
    private function editPasswordLogic ()
    {
        if (ViewData::$form_data->NewPassword !=
         ViewData::$form_data->ReNewPassword) {
            throw new Exception("对不起，两次输入的新密码不一致，请重新输入");
        }
        $info = AdminSession::getAdminSessionInfo();
        $userInfo = $this->lib->getByAdminName($info['AdminName']);
        if ($userInfo['Password'] != md5(ViewData::$form_data->OldPassword)) {
            throw new Exception("旧密码不正确，不能修改密码");
        }
        return $this->lib->setPasswordByAdminName(
        md5(ViewData::$form_data->NewPassword), $info['AdminName']);
    }
    
    public function logout(){
        AdminSession::clear();
        BaseAction::adminSuccess("登出成功",siteUrl('GuoYiSystemLogin'));
    }
}